#encoding: utf-8
load 'rds_api.rb'
load 'wechat_api.rb'
load 'tutem_utils.rb'

@rds = @rds_tt

=begin

Usage: ruby award-followup.rb TUPAL-001

1）赠送积分

2）检查指定随访计划的提交问卷，对每一份问卷给出处理提示：

判断并更新用户类型

感谢您提交图腾之友随访问卷，您的本期图腾之友计划身份为

TUPAL-001-MEMBER  正式成员，在登记日期-结算日期期间实际确认购买#盒实付#元，本期（暂无积分赠送，请留意下期随访结算提示）可获赠#积分，请在个人中心查看积分详情。
TUPAL-001-TRIAL   观察成员, 在登记日期-结算日期期间实际确认购买#盒实付#元，本期（暂无积分赠送，请留意下期随访结算提示）可获赠#积分，请在个人中心查看积分详情。
TUPAL-001-PENDING 已登记但未完成认证，请尽快完成[AAAA][BBBB]，并留意下期随访结算提示。在计划启动日期-本期结算日期间有实际购买的用户本期随访数据额外获赠2000积分
TUPAL-001-GUEST   尚未注册图腾之友计划，如果您有意长期使用图腾产品且愿意提供脱敏数据供用户及诊疗研究参考，欢迎联系客服登记图腾之友。在计划启动日期-本期结算日期间有实际购买的用户本期随访数据额外获赠2000积分奖励。

=end

#return bill_id if awarded
def award_tupal_trial uid, base_code = '', not_drill = false

    bill_id = 0
    return bill_id if uid == 0 || !(base_code.include? 'TUPAL')

    code = base_code + '-TRIAL'

    sql = "SELECT * FROM tutem_mall.eb_tupal where 
        before_count < 9 and trial_count = 0 and member_count = 0 and trial_base = 0 and member_base = 0 and since_count >= 9 
        and uid = #{uid};"

    (queryRds(sql)).each do |q|

        id = q['id']
        uid = q['uid']

        puts "award tupal trial #{uid}"

        since_count = q['since_count']
        member_count = q['member_count']
        trial_count = q['trial_count']
        since_paid = q['since_paid']
        member_base = q['member_base']
        trial_base = q['trial_base']

        available_base = since_paid - member_base - trial_base

        award_count = 9

        award_base = 0.00
        award_base = (available_base * award_count / since_count ).to_f.round(2) if since_count > 0
        puts "award base in rmb: #{award_base.to_f.round(2)}"

        bonus_points = (award_base * 0.05 * 100).to_f.round(2)
        puts "bonus in points: #{bonus_points}"

        rec = {}
        rec.store 'uid', uid
        rec.store 'code', code
        rec.store 'bonus', bonus_points
        rec.store 'channel', "#{code} 图腾之友" #这里是模板消息的交易门店
        rec.store 'brief', "观察期买满#{award_count}盒" #这里是模板消息的服务内容
        rec.store 'title', "#{code} 图腾之友观察期实付#{award_base.to_f.round(2)}" #这里是流水的mark
        rec.store 'mark', "#{code} 图腾之友观察期满#{award_count}盒" #这里是流水的mark

        ap rec if !not_drill
        next if !not_drill

        bill_id = award_points_to_user rec
        if bill_id > 0
            usql = "update tutem_mall.eb_tupal set status = 'MEMBER', trial_count = trial_count + #{award_count}, trial_base = trial_base + #{award_base} where id = #{id}"
            queryRds usql
        else
            puts "[FAIL] give points to user failed"
        end

        puts "done"

    end

    return bill_id

end

def award_tupal_member uid, base_code = '', not_drill = false

    bill_id = 0
    return bill_id if uid == 0 || !(base_code.include? 'TUPAL')

    code = base_code + '-MEMBER'

    sql = "SELECT * FROM tutem_mall.eb_tupal where ( before_count >= 9 or trial_count >= 9 ) and ((since_count - trial_count - member_count) >= 3)
        and uid = #{uid};"

    (queryRds(sql)).each do |q|

        id = q['id']
        uid = q['uid']

        puts "award tupal member #{uid}"

        since_count = q['since_count']
        member_count = q['member_count']
        trial_count = q['trial_count']
        since_paid = q['since_paid']
        member_base = q['member_base']
        trial_base = q['trial_base']

        available_base = since_paid - member_base - trial_base

        available_count = since_count - trial_count - member_count
        award_count = available_count - ( available_count % 3)

        award_base = 0.00
        award_base = award_count * available_base / available_count if available_count > 0
        puts "award base in rmb: #{award_base.to_f.round(2)}"

        bonus_points = (award_base * 0.1 * 100).to_f.round(2)
        puts "bonus in points: #{bonus_points}"

        rec = {}
        rec.store 'uid', uid
        rec.store 'code', code
        rec.store 'bonus', bonus_points
        rec.store 'channel', "#{code} 图腾之友" #这里是模板消息的交易门店
        rec.store 'brief', "计划期购买#{award_count}盒" #这里是模板消息的服务内容
        rec.store 'title', "#{code} 图腾之友计划期实付#{award_base.to_f.round(2)}" #这里是流水的mark
        rec.store 'mark', "#{code} 图腾之友计划期购买#{award_count}盒" #这里是流水的mark

        ap rec if !not_drill
        next if !not_drill

        bill_id = award_points_to_user rec
        if bill_id > 0
            usql = "update tutem_mall.eb_tupal set member_count = member_count + #{award_count}, member_base = member_base + #{award_base} where id = #{id}"
            queryRds usql
        else
            puts "[FAIL] give points to user failed"
        end

        puts "done"

    end

    return bill_id

end

def tupal_member_uids
    uids = []
    sql = "select uid from tutem_mall.eb_tupal where status = 'MEMBER'"
    (queryRds(sql)).each { |r| uids += [r['uid']] }
    return uids
end

def tupal_trial_uids
    uids = []
    sql = "select uid from tutem_mall.eb_tupal where status = 'TRIAL'"
    (queryRds(sql)).each { |r| uids += [r['uid']] }
    return uids
end

#单独奖励个别用户，注意这里trial必须在前，这样奖励成功才会成为MEMBER适用于member

=begin

uid = 7820
award_tupal_trial uid, 'TUPAL-301', true
award_tupal_member uid, 'TUPAL-301', true
exit

=end

=begin

if ARGV[0].to_s.empty?
    puts "Usage: ruby award-tupals.rb TUPAL-code [not_drill]"
    exit
else

    tupal_code = ARGV[0]
    not_drill = ARGV[1].to_s == 'not_drill' ? true : false

    puts "tupal trial #{tupal_code}"
    tupal_trial_uids.each do |uid|
        bill_id = award_tupal_trial uid, tupal_code, not_drill
    end

    puts "tupal member #{tupal_code}"
    tupal_member_uids.each do |uid|
        bill_id = award_tupal_member uid, tupal_code, not_drill
    end

end

=end

#current 2023-08-09
#current 2023-09-12
#current 2023-10-10
#current 2023-11-15
#current 2023-12-21
#current 2024-01-14
#current 2024-02-19
#current 2024-03-21
#current 2024-04-25
#current 2024-06-11 part
=begin
tupal_code = 'TUPAL-406'
not_drill = true
puts "tupal trial #{tupal_code}"
tupal_trial_uids.each do |uid|
    bill_id = award_tupal_trial uid, tupal_code, not_drill
end
puts "tupal member #{tupal_code}"
tupal_member_uids.each do |uid|
    bill_id = award_tupal_member uid, tupal_code, not_drill
end
=end
